<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
*@Name:  Subjects Controller
*
*@Author:  Ervin R. Musngi
*
*/

class Students extends CI_Controller
{
	protected $id = 0;
	
	protected $record_per_page = 0;
	
	protected $validation;
	
	public function __construct()
	{
		parent::__construct();
		
		$this->validation = array(
		
			array(
				'field'	=> 'number',
				'label'	=> 'lang:student_number',
				'rules' => 'trim|htmlspecialchars|required|max_length[10]|callback__check_student_number'
			),
			array(
				'field'	=> 'firstName',
				'label'	=> 'lang:student_fname',
				'rules'	=> 'trim|htmlspecialchars|required|max_length[20]'
			),
			array(
				'field'	=> 'lastName',
				'label'	=> 'lang:student_lname',
				'rules'	=> 'trim|htmlspecialchars|required|max_length[20]'
			),
			array(
				'field'	=> 'middleName',
				'label'	=> 'lang:student_mname',
				'rules'	=> 'trim|htmlspecialchars|required|max_length[20]'
			)
		);
		
		$this->load->library('users/auth');
		$this->load->library('form_validation');
		$this->load->library('pagination');
		$this->lang->load('student');
		
		$this->record_per_page = $this->system_library->settings['record_per_page'];
		
		$this->template
				->set_breadcrumb('Home','dashboard')
		   		->set_partial('header', 'partials/header')
				->set_partial('breadcrumb','partials/breadcrumb')
                ->set_partial('footer', 'partials/footer')
				->set_partial('navigation','partials/admin/menu')
                ->set_layout('default')
				->set_theme('default')
				->append_metadata($this->asset->css('reset.css'))
				->append_metadata($this->asset->css('general.css'))
				->append_metadata($this->asset->css('admin/default.css'))
				->append_metadata($this->asset->css('notifcation.slider.css'))
				->append_metadata($this->asset->css('admin/jquery.accordion.css'))
				->append_metadata($this->asset->js('jquery/jquery-1.6.2.min.js'))
				->append_metadata($this->asset->js('jquery.freeow.js'))
				->append_metadata($this->asset->js('notificationBox.jquery.js'))
				->append_metadata($this->asset->js('jquery_accordion_init.js'));
	}
	
	public function index()
	{
		$this->listing();
	}
	
	public function listing($page = 0)
	{
		if ($page < 0  || strlen($page) > 13)
		{
			redirect('grading/subjects/listing/');
		}
		else
		{
			$limit = $this->record_per_page;
			
			$student = new Student_m();
			
			$total_rows = $student->where('is_active',1)->get(($limit + $page),$page)->count();
			
			if($total_rows < $page)
			{
				redirect('grading/subjects/listing/');
			}
			else
			{
				$data['students']		= $student->where('is_active',1)->get($limit,$page);
				
				//setup pagination
				$config['base_url']		= site_url('grading/students/listing/');
				$config['per_page']		= $limit;
				$config['uri_segment'] 	= 4;
				$config['total_rows']	= $total_rows;
				$config['anchor_class'] = 'class="btn"';
				$config['cur_tag_open'] = ' <div class="btn btn-disabled">';
				$config['cur_tag_close'] = '</div>';
				$this->pagination->initialize($config);
			
				$data['page'] 			= $this->pagination->create_links();
				
				$this->template
							->append_metadata($this->asset->css('facebox.css'))
							->append_metadata($this->asset->js('jquery/facebox.js'))
							->set_breadcrumb('Students','')
							->build('student/list',$data);
			}
		}
	}
	
	public function create()
	{
		$item = new Item_m();
		
		$this->form_validation->set_rules($this->validation);
		
		if ($this->form_validation->run())
		{
			$student = new Student_m();
			$student->number 		= $this->input->post('number');
			$student->firstName		= $this->input->post('firstName');
			$student->lastName		= $this->input->post('lastName');
			$student->middleName	= $this->input->post('middleName');
		
			
			$student->save();
			
			$items['items'] 	= $this->input->post('student_items');
			$items['amount']	= $this->input->post('item_amount');
			
			$item->insert_item($student->id,$items);
			
			$this->session->set_flashdata('msg_title',lang('student_success_title'));
			$this->session->set_flashdata('msg',lang('success_success_insert'));
			redirect('grading/students/');
		}
		
		
		
		$data['item'] = $item->where('is_active',1)->get();
		
		if ( isAjax() )
		{
			$this->load->view('student/create',$data);
		}
		else
		{
			$this->template
						->set_breadcrumb('Subjects','grading/students/')
						->set_breadcrumb('create')
						->build('student/create',$data);
		}
	}
	
	public function _check_student_number($str)
	{

		$student = new Student_m();
		
		if ( $student->check_student_number($str) )
		{
			return true;
		}
		else
		{
			$this->form_validation->set_message('_check_student_number', lang('student_number_exist'));
			return false;
		}
		
		
	}
}